import request from "./request";

export const orderService = {
  // 获取订单列表
  async getOrderList(params) {
    const queryParams = {
      pageIndex: params.pageIndex || 1,
      pageSize: params.pageSize || 10,
    };

    if (params.status !== undefined && params.status !== null) {
      queryParams.status = params.status;
    }

    return await request({
      url: "Order/list",
      method: "GET",
      data: queryParams,
      transform: res => ({ success: true, data: res }),
    });
  },

  // 获取订单状态文本
  getStatusText(status) {
    switch (status) {
      case 0:
        return "待付款";
      case 1:
        return "待发货";
      case 2:
        return "待收货";
      case 3:
        return "待评价";
      case 4:
        return "已取消";
      case 5:
        return "已完成";
      default:
        return "未知状态";
    }
  },

  // 获取订单状态类型
  getOrderStatus(status) {
    switch (status) {
      case 0:
        return "unpaid";
      case 1:
        return "unshipped";
      case 2:
        return "unreceived";
      case 3:
        return "evaluating";
      case 4:
        return "cancelled";
      case 5:
        return "completed";
      default:
        return "unknown";
    }
  },

  // 创建订单
  async createOrder(data) {
    const orderData = {
      addressId: data.addressId,
      items: data.items.map(item => ({
        spuId: item.spuId,
        skuId: item.skuId,
        quantity: item.quantity,
        goodTitle: item.title,
        salePrice: item.salePrice * 100,
        specValues: item.specs,
        linePrice: item.linePrice ? item.linePrice * 100 : undefined,
        code: item.code,
        unit: item.unit,
      })),
      paymentType: 1,
      remark: data.remark,
      couponId: data.couponId,
      source: 1,
    };

    return await request({
      url: "Order/create",
      method: "POST",
      data: orderData,
      transform: res => {
        if (res.success && res.data) {
          const convertPrice = price => price / 100;
          res.data.totalAmount = convertPrice(res.data.totalAmount);
          res.data.payAmount = convertPrice(res.data.payAmount);
          res.data.freight = convertPrice(res.data.freight);
          res.data.discountAmount = convertPrice(res.data.discountAmount);

          if (res.data.items) {
            res.data.items = res.data.items.map(item => ({
              ...item,
              salePrice: convertPrice(item.salePrice),
              totalAmount: convertPrice(item.totalAmount),
            }));
          }
        }
        return res;
      },
    });
  },

  // 支付订单
  async payOrder(orderNo) {
    return await request({
      url: `Order/${orderNo}/pay`,
      method: "POST",
      transform: res => res.success ? {
        success: true,
        data: {
          orderNo: res.orderNo,
          amount: res.amount,
          paymentUrl: res.paymentUrl,
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
        },
      } : res,
    });
  },

  // 确认收货
  async confirmOrder(orderId) {
    return await request({
      url: `Order/${orderId}/confirm`,
      method: "POST",
    });
  },

  // 取消订单
  async cancelOrder(orderId) {
    return await request({
      url: `Order/${orderId}/cancel`,
      method: "POST",
    });
  },

  // 删除订单
  async deleteOrder(orderId) {
    return await request({
      url: `Order/${orderId}/delete`,
      method: "DELETE",
    });
  },

  // 获取订单详情
  async getOrderDetail(orderId) {
    return await request({
      url: `Order/${orderId}`,
      method: "GET",
      transform: res => {
        if (res.success && res.data) {
          const convertPrice = price => price / 100;
          res.data.totalAmount = convertPrice(res.data.totalAmount);
          res.data.payAmount = convertPrice(res.data.payAmount);
          res.data.freight = convertPrice(res.data.freight);
          res.data.discountAmount = convertPrice(res.data.discountAmount);

          if (res.data.items) {
            res.data.items = res.data.items.map(item => ({
              ...item,
              salePrice: convertPrice(item.salePrice),
              totalAmount: convertPrice(item.totalAmount),
            }));
          }
        }
        return res;
      },
    });
  },

  // 获取用户订单数量
  async getOrderCount() {
    return await request({
      url: "Order/count",
      method: "GET",
      transform: res => ({
        success: true,
        data: res.data || {},
      }),
    });
  },
};
